var express = require('express');
var router = express.Router();
var logger = require('../logs/logger');
router.use(function(req,res,next){
  console.log('Time:', Date.now());
  //此处可以存放ejs的全局变量
  // req.app.locals['userData'] = "黄小明"
  console.log("url===",req.url)
  var url = req.url;
  var sessionData = req.session.bodyData;
  if(req.url=='/login.html' || req.url=='/doLogin'){
    next();
  }else{
      if(sessionData&&sessionData.mobileNo!=''){   /*判断有没有登录*/
          next();
      }else{
        if(url == "/user" || url == '/user/a' || url == '/user/b' || url == '/user/c'){
          res.redirect('/login.html')
        }else{
          next();
        }
      }
  }  
})

/* GET home page. */
router.get('/', function(req, res, next) {
  res.render('index', { title: '首页' });
});

router.get('/university.html',function(req ,res , next){
  res.render('university', { title: '赢·公益行' });
})

router.get('/aboutUs.html',function(req, res, next){
  res.render('aboutUs',{title:'关于我们'})
})

router.get('/login.html', function(req, res, next) {
  res.render('login',{title:"登录"})
}).post('/doLogin', function(req, res, next) {
  console.log('111111111')
    var data = req.body;
    // console.log("请求数据=",data)
    req.session.bodyData = data;
    res.send(200);
    
    //模拟请求数据
    // var options = { 
    //   url: 'http://139.224.213.4:8790/token', 
    //   method: 'POST',
    //   json: true,
    //   headers: {
    //     "content-type": "application/json;charset=UTF-8",
    //   },
    //   body: { 
    //     "loginId": req.body.uname,
    //     "password": req.body.upwd,
    //     "loginSource": 'MIS'
    //   }
    //   };
    // console.log("options===",options)
    //   request(options, function(error, response, body) {
    //     if (!error && response.statusCode == 200) {
    //       //输出返回的内容
    //       console.log("请求成功=",body);
    //       req.session.bodyData = body;
    //       res.send(200);
    //     }else{
    //       console.log("请求失败=",error,body)
    //     }
    //   });
});

module.exports = router;
